package com.zsc.wxm.ysh.anjo.service.base.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger api文档配置
 *
 * @author playmaler
 * @since 2021-01-19
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 测试环境为true
     */
    private Boolean swaggerShow = true;

    private static final String TITLE = "安家时代API文档";

    @Bean
    public Docket anjoMemberApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("用户微服务接口")
                .apiInfo(anjoMemberApiInfo())
                .globalOperationParameters(Lists.newArrayList(getBuilder().build()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsc.wxm.ysh.anjo.member.controller"))
                .build();
    }

    @Bean
    public Docket anjoHouseResourcesApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("房源微服务")
                .apiInfo(anjoHouseResourcesApiInfo())
                .globalOperationParameters(Lists.newArrayList(getBuilder().build()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsc.wxm.ysh.anjo.house.controller"))
                .build();
    }

    @Bean
    public Docket anjoOssServiceApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("Oss文件微服务")
                .apiInfo(anjoOssServiceApiInfo())
                .globalOperationParameters(Lists.newArrayList(getBuilder().build()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsc.wxm.ysh.anjo.oss.controller"))
                .build();
    }

    @Bean
    public Docket anjoMsmServiceApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("短信微服务")
                .apiInfo(anjoSmsServiceApiInfo())
                .globalOperationParameters(Lists.newArrayList(getBuilder().build()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsc.wxm.ysh.anjo.sms.controller"))
                .build();
    }

    @Bean
    public Docket anjoEsServiceApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("Es微服务")
                .apiInfo(anjoEsServiceApiInfo())
                .globalOperationParameters(Lists.newArrayList(getBuilder().build()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsc.wxm.ysh.anjo.es.controller"))
                .build();
    }


    private ApiInfo anjoMemberApiInfo() {
        return new ApiInfoBuilder().title(TITLE)
                .description("用户微服务接口")
                .version("1.0")
                .build();
    }

    private ApiInfo anjoHouseResourcesApiInfo() {
        return new ApiInfoBuilder().title(TITLE)
                .description("房源微服务")
                .version("1.0")
                .build();
    }

    private ApiInfo anjoOssServiceApiInfo() {
        return new ApiInfoBuilder().title(TITLE)
                .description("文件微服务")
                .version("1.0")
                .build();
    }

    private ApiInfo anjoSmsServiceApiInfo() {
        return new ApiInfoBuilder().title(TITLE)
                .description("短信微服务")
                .version("1.0")
                .build();
    }

    private ApiInfo anjoEsServiceApiInfo() {
        return new ApiInfoBuilder().title(TITLE)
                .description("ES微服务")
                .version("1.0")
                .build();
    }


    private ParameterBuilder getBuilder() {
        ParameterBuilder builder = new ParameterBuilder();
        builder.parameterType("header").name("token")
                .description("token参数(测试阶段不用传)")
                .required(false)
                // 在swagger里显示header
                .modelRef(new ModelRef("string"));
        return builder;
    }
}
